package hu.ikti.oamadmin.beans.utils;

import hu.ikti.oamadmin.beans.BaseBean;
import hu.ikti.oamadmin.types.enums.Month;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Vector;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("dateUtil")
@Scope(ScopeType.SESSION)
public class DateUtil extends BaseBean implements Serializable {

	private static final long serialVersionUID = 1L;

	public List<SelectItem> getDayItems(int month, int year) {
		GregorianCalendar calendar = new GregorianCalendar();

		List<SelectItem> items = new Vector<SelectItem>();

		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONDAY, month - 1);

		int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		for (int i = 1; i <= maxDay; i++) {
			SelectItem item = new SelectItem(i, Integer.toString(i));
			items.add(item);
		}
		return items;
	}

	public List<SelectItem> getMonthItems() {
		List<SelectItem> items = new Vector<SelectItem>();

		Month[] months = Month.values();

		for (Month month : months) {
			SelectItem item = new SelectItem(month.getMonth(), month.name());
			items.add(item);
		}
		return items;
	}

	public List<SelectItem> getYearItems() {
		Calendar calendar = new GregorianCalendar();

		List<SelectItem> items = new Vector<SelectItem>();

		for (int i = 1900; i < calendar.get(Calendar.YEAR); i++) {
			SelectItem item = new SelectItem(i, Integer.toString(i));
			items.add(item);
		}
		return items;
	}

	public static Date createDate(int day, int month, int year)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String date = day + "/" + month + "/" + year;
		return formatter.parse(date);

	}

}
